import yaml
import requests
import os


def read_meta_data(filename="收录数据.yaml"):
    with open(filename, "r", encoding="utf-8") as f:
        meta_data = yaml.safe_load(f)
    return meta_data


def download_file(url, filename, over_write=False):
    if os.path.exists(filename) and not over_write:
        print(f'文件已存在: {filename}')
        return
        
    try:
        with requests.get(url, stream=True) as response:
            print(f'开始下载: {url}')
            response.raise_for_status()
            total_size = int(response.headers.get('content-length', 0))
            
            downloaded_size = 0
            with open(filename, 'wb') as f:
                for chunk in response.iter_content(chunk_size=8192):
                    if chunk:
                        f.write(chunk)
                        downloaded_size += len(chunk)
                    # 计算并显示进度
                    if total_size > 0:
                        progress = downloaded_size / total_size * 100
                        print(f'下载进度: {progress:.1f}% ({downloaded_size/1024/1024:.2f} MB / {total_size/1024/1024:.2f} MB)', end='\r')
        
        print(f'\n文件已下载到: {os.path.abspath(filename)}')
    except requests.exceptions.RequestException as e:
        print(f'下载失败: {e}')


if __name__ == "__main__":
    # 读取收录数据.yaml,并下载所有数据
    # 如果文件已存在,则不下载（通过默认参数设置over_write=False）
    meta_data = read_meta_data()
    for item, item_info in meta_data.items():
        print(item)
        for cn_name, li in item_info.items():
            filename = li[0]
            url = li[1]
            print(filename, url)
            download_file(url, filename)